﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ARA.Domain.Datas
{
    public class Person
    {
        public List<JoinSegment> Segments { get; set; }

        public List<DayRecord> DayRecords { get; set; }

        public string Account { get; set; }

        public string Name { get; set; }

        public Person(string account, string name)
        {
            this.Account = account;
            this.Name = name;
            Segments = new List<JoinSegment>();

            DayRecords = new List<DayRecord>();
        }

        public DayRecord GetDayRecord(DateTime date)
        {
            DayRecord record = null;
            for (int index = 0; index < DayRecords.Count; index++)
            {
                if (DayRecords[index].Year == date.Year && DayRecords[index].Month == date.Month
                    && DayRecords[index].Day == date.Day)
                {
                    record = DayRecords[index];
                    break;
                }
            }
            return record;
        }

        /// <summary>
        /// 根据当前时间获取对应的日记录
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private DayRecord GetOrCreateDayRecord(DateTime date)
        {
            DayRecord record = null;
            for(int index = 0; index < DayRecords.Count; index++)
            {
                if(DayRecords[index].Year == date.Year && DayRecords[index].Month == date.Month
                    && DayRecords[index].Day == date.Day)
                {
                    record = DayRecords[index];
                    break;
                }
            }

            if(record == null)
            {
                record = new DayRecord(date.Year, date.Month, date.Day);
                DayRecords.Add(record);
            }

            return record;
        }

        public void AddSegment(DateTime joinTime, DateTime endTime)
        {
            if(joinTime.Day != endTime.Day)
            {
                // 跨天了
                // 拆分成多天
                for(int index = joinTime.Day ; index <= endTime.Day; index++)
                {
                    int delta = index - joinTime.Day;
                    if(index == joinTime.Day)
                    {
                        DateTime tJoinTime = joinTime;
                        DateTime tEndTime = new DateTime(joinTime.AddDays(delta).Year,
                            joinTime.AddDays(delta).Month,
                            joinTime.AddDays(delta).Day,
                            23, 59, 59);
                        DayRecord record = GetOrCreateDayRecord(tJoinTime);
                        record.AddSegment(true, tJoinTime, tEndTime);
                    }
                    else if(index == endTime.Day)
                    {
                        DateTime tJoinTime = new DateTime(joinTime.AddDays(delta).Year,
                            joinTime.AddDays(delta).Month,
                            joinTime.AddDays(delta).Day,
                            0, 0, 0);
                        DateTime tEndTime = endTime;
                        DayRecord record = GetOrCreateDayRecord(tJoinTime);
                        record.AddSegment(true, tJoinTime, tEndTime);
                    }
                    else
                    {
                        DateTime tJoinTime = new DateTime(joinTime.AddDays(delta).Year,
                            joinTime.AddDays(delta).Month,
                            joinTime.AddDays(delta).Day,
                            0, 0, 0);
                        DateTime tEndTime = new DateTime(joinTime.AddDays(delta).Year,
                            joinTime.AddDays(delta).Month,
                            joinTime.AddDays(delta).Day,
                            23, 59, 59);
                        DayRecord record = GetOrCreateDayRecord(tJoinTime);
                        record.AddSegment(true, tJoinTime, tEndTime);
                    }
                }
            }
            else
            {
                DayRecord record = GetOrCreateDayRecord(joinTime);
                record.AddSegment(false, joinTime, endTime);
            }
        }
    }
}
